package com.glsx.core.common;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.glsx.core.entity.ErrorInfo;
import com.glsx.core.entity.MyException;

/**
 * 创建全局异常处理类：通过使用@ControllerAdvice定义统一的异常处理类，
 * 而不是在每个Controller中逐个定义。
 * @ExceptionHandler用来定义函数针对的异常类型，
 * 最后将Exception对象和请求URL映射到error.html中
 * @author xiangtao
 *
 */
@ControllerAdvice
public class GlobalExceptionHandler {
	 
	 /**
	  * 直接返回异常页面
	  */
	 public static final String DEFAULT_ERROR_VIEW = "error";
	    @ExceptionHandler(value = Exception.class)
	    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
	        ModelAndView mav = new ModelAndView();
	        mav.addObject("exception", e);
	        mav.addObject("url", req.getRequestURL());
	        mav.setViewName(DEFAULT_ERROR_VIEW);
	        return mav;
	  }
	    
	  /**
	   * 返回以json格式返回异常  
	   */
	 @ExceptionHandler(value = MyException.class)
	 @ResponseBody
	 public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
	        ErrorInfo<String> r = new ErrorInfo<>();
	        r.setMessage(e.getMessage());
	        r.setCode(ErrorInfo.ERROR);
	        r.setData("Some Data");
	        r.setUrl(req.getRequestURL().toString());
	        return r;
	  }
	    
	    
}
